#include <iostream>
#include <fstream>
#include "buffer.hpp"

int main()
{
    std::ifstream ifs("./logfile/test1.log", std::ios::binary);
    if(ifs.is_open() == false)
    {
        std::cout << "文件打开失败!--1" << std::endl;
        return -1;
    }
    ifs.seekg(0, std::ios::end); //读写位置跳转到文件末尾
    size_t fsize = ifs.tellg(); //获取当前读写位置相对于起始位置的偏移量
    ifs.seekg(0, std::ios::beg);//重新跳转到起始位置
    std::string body(fsize, 0);
    ifs.read(&body[0], fsize);
    if(ifs.good() == false)
    {
        std::cout << "read error!" << std::endl;
    }
    std::cout << fsize << std::endl;

    //写入到缓冲区中
    Log::Buffer buffer(Log::BufferType::DYNAMICGROW);
    for (int i = 0; i < body.size(); ++i)
    {
        //每次写一个字符的数据
        buffer.push(&body[i], 1);
    }
    std::cout << buffer.readAbleSize() << std::endl;

    //将缓冲区中的数据再写入到另一个文件中
    std::ofstream ofs("./tmp.log", std::ios::binary);
    if (ofs.is_open() == false)
    {
        std::cout << "文件打开失败!---2" << std::endl;
        return -1;
    }
    while (buffer.readAbleSize() != 0)
    {
        ofs.write(buffer.begin(), 1);
        if (ofs.good() == false)
        {
            std::cout << "write error!" << std::endl;
        }
        buffer.moveReader(1);
    }

    //关闭文件
    ofs.close();
    ifs.close();
    return 0;
}